package cn.tedu.management.portal.controller;


import cn.tedu.management.portal.dto.DepartmentDTO;
import cn.tedu.management.portal.dto.SelectDeptDTO;
import cn.tedu.management.portal.model.Department;
import cn.tedu.management.portal.service.IDepartmentService;
import cn.tedu.management.portal.util.R;
import cn.tedu.management.portal.vo.DepartmentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/portal/department")
public class DepartmentController {

    @Autowired
    IDepartmentService departmentService;

    //http://localhost:8080/portal/department/add?pId=1&department=development&manager=Tom&phone=15296345174&email=tom@qq.com&state=1
    //增加一个新部门
    @RequestMapping("/add")
    public R<Void> addDepartment(DepartmentDTO departmentDTO){
        departmentService.addDepartment(departmentDTO);
        return R.ok();
    }

    //http://localhost:8080/portal/department/show/update?id=1
    //展示修改页部门详情
    @RequestMapping("/show/update")
    public R<DepartmentVO> showDeptById(Integer id){
        return R.ok(departmentService.showDeptById(id));
    }

    //http://localhost:8080/portal/department/update?id=2&pId=0&department=dept&manager=andy&phone=15296345174&email=andy@qq.com&state=1
    //修改部门信息
    @RequestMapping("/update")
    public R<Void> updateDepartment(DepartmentDTO departmentDTO){
        departmentService.updateDepartment(departmentDTO);
        return R.ok();
    }

    //http://localhost:8080/portal/department/show/all
    //展示全部部门
    @RequestMapping("/show/all")
    public R<List<Department>> showDepartments(){
        List<Department> departments=departmentService.showDepartments();
        return R.ok(departments);
    }

    //http://localhost:8080/portal/department/delete?id=1
    //删除一个部门
    @RequestMapping("/delete")
    public R<Void> deleteDeptById(Integer id){
        departmentService.deleteDeptById(id);
        return R.ok();
    }

    //http://localhost:8080/portal/department/find?department=业务部&state=1
    //根据部门名称及部门状态查找部门详情
    @RequestMapping("/find")
    public R<Department> findADept(SelectDeptDTO selectDeptDTO){
        Department department=departmentService.findADept(selectDeptDTO);
        return R.ok(department);
    }

}

